package cn.tedu.property2022.service.impl;

import cn.tedu.property2022.ex.ServiceException;
import cn.tedu.property2022.mapper.AnnounucementsMapper;
import cn.tedu.property2022.pojo.dto.AnnouncementsAddNewDTO;
import cn.tedu.property2022.pojo.entity.Announcements;
import cn.tedu.property2022.pojo.vo.AnnouncementsLsitVO;
import cn.tedu.property2022.pojo.vo.AnnouncementsVO;
import cn.tedu.property2022.service.IAnnouncementsService;
import cn.tedu.property2022.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class AnnouncementsServiceIpml implements IAnnouncementsService {

    @Autowired
    private AnnounucementsMapper announucementsMapper;

    @Value("${custom.file-upload.resource-host}")
    private String url;

    @Override
    public void addNew(AnnouncementsAddNewDTO announcementsAddNewDTO) {
        String title=announcementsAddNewDTO.getTitle();
        AnnouncementsVO annonucements = announucementsMapper.getAnnonucements(title);
        if (annonucements!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"该公告已存在!");
        }
        String path=announcementsAddNewDTO.getPath();
        Announcements announcemen=new Announcements();
        BeanUtils.copyProperties(announcementsAddNewDTO,announcemen);
        LocalDateTime now=LocalDateTime.now();
        announcemen.setPath(url+path);
        announcemen.setGmtModified(now);
        announcemen.setGmtCreate(now);
        int row = announucementsMapper.insertAnn(announcemen);
        if (row!=1){
           throw new ServiceException(ServiceCode.ERR_INTERNAL_SERVER_ERROR,"该公告未发不成功请，联系联系管理员");
        }
    }

    @Override
    public List<AnnouncementsVO> Annoucements(String title) {
        return announucementsMapper.AnnonucementsVOList(title);
    }

    @Override
    public List<AnnouncementsLsitVO> list() {
     return announucementsMapper.AnnonucementsList();
    }

    @Override
    public List<AnnouncementsVO> getMessage(Long id) {
        return announucementsMapper.getMessage(id);
    }


}
